package vnc.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import jpcap.packet.*;


//import jdumper.PacketBox;

import javax.swing.tree.DefaultMutableTreeNode;


import vnc.JDPacketAnalyzerLoader;
import vnc.VncFrame;
import vnc.analyzer.EthernetAnalyzer;
import vnc.analyzer.IPv4Analyzer;
import vnc.analyzer.JDPacketAnalyzer;



public class AddrTable extends JLayeredPane implements Runnable{
	
	public static AddrBox tab;
	public static JLabel jlLeft = null;
	public static JLabel jlRight = null;
	public static PacketBox sBox;
	public static String iph,mach;

	public static Thread t;
	public JScrollPane jp,jp2,jp3;
	public JPanel macPane,ipPane;
	
	//add.
	public Hashtable<String,Integer> macTable;
	public Hashtable<String,Integer> ipTable;
	
	//add
	//modify
	public static int mtableSize =1,m=1;
	public static int itableSize =1,i=1;
	static int size=0;
	//modify
	public final List<AddrBox> tables;
	public final List<AddrBox> mtables;
	
	final List<Packet> packets;
	
	List<JDPacketAnalyzer> analyzers;

	public AddrTable(List<Packet> packets, String mac,String ip){

		this.mach = mac;
		this.iph = ip;
		t = new Thread(this, "Address Table");
		
		macTable = new Hashtable<String,Integer>();
		ipTable = new Hashtable<String,Integer>();
		
		this.packets = packets;
		//modify
		tables = new ArrayList<AddrBox>();
		
		//add
		mtables = new ArrayList<AddrBox>();
		
		analyzers = JDPacketAnalyzerLoader.getAnalyzers();

		ipTable.put("null", 0);		
		init();
	}

	
	private ImageIcon getImageIcon(final String path){
		return (new ImageIcon(this.getClass().getResource(path)));
	}
	
	private ImageIcon scaleImageIcon(ImageIcon icon, final int width, final int height){
		Image img = icon.getImage();
		
		Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
		
		icon = new ImageIcon(newimg);

		return icon;
	}

	public void init(){
		this.setLayout(null);
		
		jp = new JScrollPane();
		jp.setBackground(Color.white);
		jp.setLayout(null);

		//add
		
		jp2 = new JScrollPane();
		jp2.setBackground(Color.white);
		
		jp3 = new JScrollPane();
		jp3.setBackground(Color.white);

		this.add(jp, new Integer(4), 0);
		this.add(jp2, new Integer(4), 0);
		this.add(jp3, new Integer(4), 0);
	
		jp.setBounds(0, 0, 341, 370);
		jp2.setBounds(342, 0, 341, 370);
		jp3.setBounds(684, 0, 340, 370);
		
		jp.setBorder(BorderFactory.createEmptyBorder());
		jp2.setBorder(BorderFactory.createEmptyBorder());
		jp3.setBorder(BorderFactory.createEmptyBorder());
		
		
		jlRight = new JLabel(getImageIcon("/images/Right.png")); 
		
			
		jlLeft = new JLabel(getImageIcon("/images/Left.png"));
		this.add(jlRight, new Integer(5), 0);
		this.add(jlLeft, new Integer(5), 0);
		
		jlRight.setBounds(0, 130,1024, 100);
		jlLeft.setBounds(0, 130,1024, 100);
		jlLeft.setVisible(false);
		jlRight.setVisible(false);
		
		macPane = new JPanel();		//jp2에 들어갈 맥팬
		ipPane = new JPanel();		//jp3에 들어갈 아이피팬
		
		
		macPane.setBackground(Color.white);
		macPane.setLayout(null);
		
		ipPane.setBackground(Color.white);
		ipPane.setLayout(null);
	
		jp3.setViewportView(ipPane);
		jp2.setViewportView(macPane);
		//수직스크롤바만 가능.
		jp2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		jp2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		jp3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		jp3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			
		//this.initbox();
		/*
		AddrBox ab = new AddrBox();
		ab.index = 0;
		ab.ipName = null;
		ab.mName = "ff:ff:ff:ff:ff:ff";
		mtables.add(ab);
		tables.add(ab);
		ipTable.put("null", 0);
		macTable.put(ab.mName, 0);
		
		tab = new AddrBox();
		tab.ipName = iph;
		tab.mName = mach;
		jp.add(makeIcon(tab,3));	
		
		
		
		try {
			setBoxBounds2(mtables.get(0));
			setBoxBounds(tables.get(0));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	public void initbox(){
	
		AddrBox ab = new AddrBox();
		ab.index = 0;
		ab.ipName = null;
		ab.mName = "ff:ff:ff:ff:ff:ff";
		mtables.add(ab);
		tables.add(ab);
		ipTable.put("null", 0);
		macTable.put(ab.mName, 0);
		
		tab = new AddrBox();
		tab.ipName = iph;
		tab.mName = mach;
		jp.add(makeIcon(tab,3));	
		
		
		
		try {
			setBoxBounds2(mtables.get(0));
			setBoxBounds(tables.get(0));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public JButton makeIcon(AddrBox ab,int c)
	{
	
		JButton b=null;
		if(c==1)
		{
			b = new JButton("    "+ab.ipName, scaleImageIcon(getImageIcon("/images/computer.png"), 50, 50));
			b.setFont(new Font(ab.ipName, Font.BOLD, 16));
			b.setBounds(MovingBox.startP3.x,MovingBox.startP3.y, MovingBox.currentD3.width, MovingBox.currentD3.height);
			
		}
		else if(c==0)
		{
			
			b = new JButton("    "+ab.mName, scaleImageIcon(getImageIcon("/images/computer.png"), 50, 50));
			b.setFont(new Font(ab.mName, Font.BOLD, 16));
			b.setBounds(MovingBox.startP2.x,MovingBox.startP2.y, MovingBox.currentD2.width, MovingBox.currentD2.height);
		}
		else
		{
			b = new JButton("<html>"+ab.ipName+"<br></br>"+ ab.mName+"</html>", scaleImageIcon(getImageIcon("/images/computer.png"), 50, 50));
			b.setFont(new Font("", Font.BOLD, 16));
			b.setBounds(MovingBox.startP2.x,MovingBox.startP2.y, MovingBox.currentD2.width, MovingBox.currentD2.height);
			
		}
		
		b.setPressedIcon(scaleImageIcon(getImageIcon("/images/computer_pressed.png"), 50, 50));
		b.setRolloverIcon(scaleImageIcon(getImageIcon("/images/computer_rollover.png"), 50, 50));
		b.setVerticalTextPosition(SwingConstants.CENTER);
        b.setHorizontalTextPosition(SwingConstants.RIGHT);
       
        removeButtonBorder(b);
       
		return b;
	}
	private void removeButtonBorder(JButton jb){
		jb.setBorderPainted(false);
		jb.setContentAreaFilled(false);
		jb.setDefaultCapable(false);
		jb.setFocusPainted(false);
	}
	public void setAddress(Packet p){
		AddrBox ab = new AddrBox();
		
			
		for(JDPacketAnalyzer analyzer:analyzers){
			
			if(analyzer.isAnalyzable(p))
			{
				analyzer.analyze(p);
				
				if(analyzer.layer == 0)
				{
		
					EthernetAnalyzer eth = new EthernetAnalyzer();
					eth.analyze(p);
					ab.index = m;
					ab.mName = eth.getValue("Destination MAC").toString();			
					ab.ipName= "null";
					if(!ab.mName.equals(mach))
					{
						if(!macTable.containsKey(ab.mName)){
							macTable.put(ab.mName,m++);
							mtables.add(ab);
							mtableSize = m-1;
							try {
								//System.out.println(mtables.isEmpty()+  "  "+ mtableSize);
								setBoxBounds2(mtables.get(mtableSize));
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
				if(analyzer.layer == 1 && !(analyzer.getProtocolName().equals("ARP_RARP")))
				{
		
					IPv4Analyzer ip = new IPv4Analyzer();
					ip.analyze(p);
					ab.index = itableSize;
					ab.ipName= ip.getValue("Destination IP").toString();
					
					if(!iph.equals(ab.ipName))
					{
						if(!ipTable.containsKey(ab.ipName)){
							ipTable.put(ab.ipName,i++);
							tables.add(ab);
							itableSize = i-1;	
							try {
								setBoxBounds(tables.get(itableSize));
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
			
		}
		size++;	
	
}	
	
	public void mappingTable(){
	
			if(sBox.packet!=null){
				
				if(macTable.containsKey(sBox.mName))
				{
					
					MovingBox.currentItem3 = macTable.get(sBox.mName);
					adjustAllBoxesLocation2();}
					
				System.out.println();
					if(sBox.pName.equals("ARP_RARP")){
					
					MovingBox.currentItem2 = 0;
					adjustAllBoxesLocation();
					}
				
					else if(ipTable.containsKey((sBox.ipName))){
					
						MovingBox.currentItem2 = ipTable.get(sBox.ipName);
						adjustAllBoxesLocation();
					}
			
			}
	}
	
	public void saveTable() {
		this.initbox();
		for(int j=0;j<this.packets.size();j++)
		{
				setAddress(packets.get(j));
				revalidate();
				repaint();
						
		}
		
		
	}
	
	public void setBoxBounds(final AddrBox box) throws InterruptedException{
		
		JButton b=null;
		b = makeIcon(box,1);
		if(tables.indexOf(box) == 0){
			
			b.setBounds(MovingBox.startP2.x,MovingBox.startP2.y, MovingBox.currentD2.width, MovingBox.currentD2.height);
		}else{
			if(tables.indexOf(box) == MovingBox.currentItem2){
				b.setBounds(MovingBox.currentP2.x,tables.get(tables.indexOf(box)-1).getLocation().y + tables.get(tables.indexOf(box)-1).getSize().height,
						MovingBox.currentD2.width, MovingBox.currentD2.height);

			}else{
				
				b.setBounds(MovingBox.startP2.x,tables.get(tables.indexOf(box)-1).getLocation().y + tables.get(tables.indexOf(box)-1).getSize().height,
						MovingBox.currentD2.width, MovingBox.currentD2.height);
			}
		}
	
		adjustAllBoxesLocation();
			ipPane.add(b);
			ipPane.setPreferredSize(new Dimension(0,MovingBox.currentD2.height + (tables.size()-1)*MovingBox.currentD2.height + (MovingBox.spaceD2.height*2)));

	}
	
	public void adjustAllBoxesLocation(){
		{
			for(int i=0; i < tables.size(); i++){
				if(i == 0){
				tables.get(i).setBounds(MovingBox.startP2.x, MovingBox.startP2.y, MovingBox.currentD2.width, MovingBox.currentD2.height);
				
				}else{
				tables.get(i).setBounds(MovingBox.startP2.x,tables.get(i-1).getLocation().y + tables.get(i-1).getSize().height,
							MovingBox.currentD2.width, MovingBox.currentD2.height);
			}
		}
	}
		jp3.getVerticalScrollBar().setValue(tables.get(MovingBox.currentItem2).getLocation().y - MovingBox.spaceD2.height);
		
	}
	
	
	public void setBoxBounds2(final AddrBox box) throws InterruptedException{
		
		JButton b=null;
		b = makeIcon(box,0);
		if(mtables.indexOf(box) == 0){
			
			b.setBounds(MovingBox.startP3.x,MovingBox.startP3.y, MovingBox.currentD3.width, MovingBox.currentD3.height);
	
		}else{
		
			if(mtables.indexOf(box) == MovingBox.currentItem3){			
				b.setBounds(MovingBox.currentP3.x,mtables.get(mtables.indexOf(box)-1).getLocation().y + mtables.get(mtables.indexOf(box)-1).getSize().height,
						MovingBox.currentD3.width, MovingBox.currentD3.height);

			}else{
				b.setBounds(MovingBox.startP3.x,mtables.get(mtables.indexOf(box)-1).getLocation().y + mtables.get(mtables.indexOf(box)-1).getSize().height,
						MovingBox.currentD3.width, MovingBox.currentD3.height);
			}
		}
		
		adjustAllBoxesLocation2();
			macPane.add(b);
			macPane.setPreferredSize(new Dimension(0,MovingBox.currentD2.height + (mtables.size()-1)*MovingBox.currentD2.height + (MovingBox.spaceD2.height*2)));

	}

	public void adjustAllBoxesLocation2(){
		
			for(int i=0; i < mtables.size(); i++){
				if(i == 0){

						mtables.get(i).setBounds(MovingBox.startP3.x, MovingBox.startP3.y, MovingBox.currentD3.width, MovingBox.currentD3.height);
				
				}else{
						mtables.get(i).setBounds(MovingBox.startP3.x,mtables.get(i-1).getLocation().y + mtables.get(i-1).getSize().height,
							MovingBox.currentD3.width, MovingBox.currentD3.height);
					 
			}
		
	}
		jp2.getVerticalScrollBar().setValue(mtables.get(MovingBox.currentItem3).getLocation().y - MovingBox.spaceD3.height);
		
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
}


